package com.cy.milktea.controller;


import com.cy.milktea.pojo.System.JsonResult;
import com.cy.milktea.pojo.Menu;
import com.cy.milktea.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @PostMapping
    public JsonResult add(@RequestBody Menu menu) {
        if (menu.getParentId() == null || menu.getName() == null || menu.getSort() == null) {
            return JsonResult.error("参数不全!");
        }
        menuService.add(menu);
        return JsonResult.ok("添加成功!");
    }

    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable Integer id) {
        int delete = menuService.delete(id);
        if (delete == 0) {
            return JsonResult.error("当前菜单id不存在!");
        } else if (delete == -1) {
            return JsonResult.error("有角色含有该菜单,不可删除!");
        }
        return JsonResult.ok("删除成功!");
    }

    @PutMapping
    public JsonResult update(@RequestBody Menu menu) {
        return menuService.update(menu) == 1 ? JsonResult.ok("更新成功!") : JsonResult.error("菜单id不存在!");
    }

    @GetMapping("/all")
    public JsonResult queryAll() {
        return JsonResult.ok("查询成功!", menuService.queryAll());
    }

    @GetMapping("/{roleId}")
    public JsonResult queryByRoleId(@PathVariable Integer roleId){
        return JsonResult.ok("查询当前角色菜单成功!",menuService.queryByRoleId(roleId));
    }
}













